package com.cqc.textureview01;

import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.widget.FrameLayout;
//import android.widget.LinearLayout;

import java.io.IOException;

/**
 * Texture + Camera
 * 1 open()
 * 2 获取 size
 * 3 设置layParams
 * 4 给Camera设置Texture
 * 5 预览
 * 6 启动
 */
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {

    private TextureView textureView;
    private Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textureView = (TextureView) findViewById(R.id.textureView);

        textureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        //android.hardware.Camera 已过时
        camera = Camera.open();
        Camera.Size pictureSize = camera.getParameters().getPictureSize();
        //xml中用的FrameLayout，这里也必须是FrameLayout
//        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//        textureView.setLayoutParams(layoutParams);
        try {
            camera.setPreviewTexture(surface);
            camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }

//        textureView.setAlpha(0.5f);
//        textureView.setRotation(70.0f);//旋转
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        camera.stopPreview();//顺序不可乱
        camera.release();
        return true;//默认false
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
}
